package com.china.produce.bdregistrar;

import com.china.produce.pojo.Users;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 实现importBeanDefinitionRegistrar,往容器中自定义添加bean
 * 注册bean
 */
public class UserImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder
                .rootBeanDefinition(Users.class)
                // 设置 User 这个Bean的属性username的值为三友的java日记
                .addPropertyValue("account", "15618298639")
                .addPropertyValue("password", "1q2w3e")
                .addPropertyValue("userName", "nanjing")
                .addPropertyValue("id", 1000)
                .addPropertyValue("perms", "诗人")
                .getBeanDefinition();

         //把 User 这个Bean的定义注册到容器中
        registry.registerBeanDefinition("users", beanDefinition);
    }
}
